package com.widevision.ListActivity;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import net.widevision.android.R;
import net.widevision.android.TwitterApp;
import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.User;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.widevision.adapter.FrndListAdapter;
import com.widevision.adapter.FrndListBean;

public class FrndName extends Activity{
	
	private static final String twitter_consumer_key = "JXmXcE5yFPLIRCCtQ79eg";
	private static final String twitter_secret_key = "dnVojAwaewvEt8E2QwLYtavwm8cdUHwph7lZCaQPMY";
	//***********************************//
	private AQuery aq;
	 //***********************************//
    FrndListAdapter frndListAdapter;
	
    FrndListBean frndListBean;
    
    ArrayList<Drawable> frndimg;
    ArrayList<String> frndlist;
    ArrayList<Object> itemfrndlist;
    
    ResponseList<twitter4j.User> frndlisttext;
    
    User user,user1;
    
    TwitterApp mTwitter;
    
    InputStream is;
    
    Drawable frndsimg;
    
    ListView frndlistview;
    
    TextView Scroll;
    
    String frndname,screeName;
    
    String userId,userid;
    
    int Id;
    
    int id;
    
    int count;
    
    int Userid;
    
    Twitter tTwitter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//***********************************//
		aq = new AQuery(this);
		//***********************************//
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frndlist);
		
		frndlistview    =(ListView) findViewById(R.id.frndidlist);
		Scroll          =(TextView) findViewById(R.id.scroller1);
				
		Bundle bundle=getIntent().getExtras();
			
		Userid=bundle.getInt("USERID");

		try {
			prepareArralist();
		} catch (IllegalStateException e) {
			
			e.printStackTrace();
		}
		
		
		//aq.progress(ApplicationConstants.dialog(this)).ajax(url, JSONObject.class, NotificationList.this, "jsonCallback");
		frndListAdapter  = new FrndListAdapter(FrndName.this, itemfrndlist,aq);
		
		
		frndlistview.setAdapter(frndListAdapter);

		Scroll.setSelected(true);
		
		Scroll.setText("@"+screeName+" "+"is Following"+"                                                      ");
		
		frndlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
				
				TextView txt = (TextView) view.findViewById(R.id.userId);
				
				userid=txt.getText().toString();
			 
			    Intent intent=new Intent(FrndName.this, UserProfile.class);
			    
			    id=Integer.parseInt(userid);
			    
			    intent.putExtra("USERID", id);
			    
			    startActivity(intent);
			}
		});
	}

	private void prepareArralist()  {
		
		    itemfrndlist=new ArrayList<Object>();
		    
		    mTwitter 	= new TwitterApp(this, twitter_consumer_key,twitter_secret_key);
		    
			if (mTwitter.hasAccessToken()){
				 
	        user=mTwitter.showUser(Userid);
	        
	        screeName=user.getScreenName();

  		    count=user.getFriendsCount();
		
			frndlisttext=mTwitter.Friendsids(Userid);
			
			try
			{
				for(int i=0;i<count;i++)
				{
					
					user1=frndlisttext.get(i);
				
					Id=user1.getId();
				
					userId=String.valueOf(Id);
				
					frndname=user1.getScreenName();
				
					URL u=user1.getProfileImageURL();
				
					String frndsimg=String.valueOf(u);
					Log.d("image string value>>>>>>>>>>>>", frndsimg);
//				try {
//						is = u.openStream();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//				frndsimg = Drawable.createFromStream(is, "src name");

				AddObjectToList(frndname,frndsimg,userId);
				}
			} catch (Exception e) {
				e.printStackTrace();
		  }
	    }
    }

	private void AddObjectToList(String frndname,String frndsimg,String userId) {
		frndListBean=new FrndListBean();
		frndListBean.setName(frndname);
		frndListBean.setImage(frndsimg);
		frndListBean.setUserId(userId);
		itemfrndlist.add(frndListBean);
		}
   }
